home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / HeapProc.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  4KB  |  170 lines

  1. /* HeapProc.c -- Implementation of Process with stack on free storage heap
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     January, 1988
  20.  
  21. Function:
  22.     
  23. Modification History:
  24.  
  25. $Log:    HeapProc.c,v $
  26.  * Revision 3.0  90/05/20  00:19:45  kgorlen
  27.  * Release for 1st edition.
  28.  * 
  29. */
  30.  
  31. #include "HeapProc.h"
  32. #include "Scheduler.h"
  33. #include "nihclIO.h"
  34.  
  35. #define    THIS    HeapProc
  36. #define    BASE    Process
  37. #define    BASE_CLASSES BASE::desc()
  38. #define    MEMBER_CLASSES
  39. #define    VIRTUAL_BASE_CLASSES
  40.  
  41. extern const int NIHCL_BADTRACETBL,NIHCL_STACKOV;
  42.  
  43. DEFINE_ABSTRACT_CLASS(HeapProc,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/HeapProc.c,v 3.0 90/05/20 00:19:45 kgorlen Rel $",NULL,NULL);
  44.  
  45. HeapProc::HeapProc(const char* name, stackTy* bottom, int priority, unsigned long size)
  46.     : BASE(name, bottom, priority)
  47. {
  48.     stack_size = size; 
  49. }
  50.  
  51. HeapProc::HeapProc(OIOifd& fd) : BASE(fd) {}
  52.  
  53. HeapProc::HeapProc(OIOin& strm) : BASE(strm) {}
  54.  
  55. HeapProc::~HeapProc()
  56. {
  57.     delete stack_bottom;
  58. }
  59.  
  60. void HeapProc::setupStack()
  61. {
  62.     stackTy* top = topOfStack();
  63.     stackTy* bottom = stack_bottom;
  64.     stack_bottom = new stackTy[stack_size];
  65.  
  66. #ifdef sparc
  67.     asm("t    0x03");        // ST_FLUSH_WINDOWS
  68. #endif
  69.  
  70. #if STACK_GROWS_DOWN
  71.     stack_bottom[0] = (stackTy)UNINITIALIZED;
  72.     unsigned long size = bottom-top;
  73.     copyStack(top+1, stack_bottom+stack_size-size, size);
  74.     long delta = (stack_bottom+stack_size-1 - bottom) * sizeof(stackTy);
  75. #else
  76.     stack_bottom[stack_size-1] = (stackTy)UNINITIALIZED;
  77.     unsigned long size = top-bottom;
  78.     copyStack(bottom, stack_bottom, size);
  79.     long delta = (stack_bottom - bottom) * sizeof(stackTy);
  80. #endif
  81.  
  82. #if defined(mc68000) || defined(sparc)
  83.     SP() += delta;    // adjust stack pointer
  84.     FP() += delta;    // adjust frame pointer
  85. #endif
  86.  
  87. #ifdef ibm032
  88.  
  89. #ifdef UCB43BSD
  90. /* jmpbuf layout:
  91.  
  92.     jmpbuf[0]    r1    stack pointer
  93.     jmpbuf[1]    r6
  94.     jmpbuf[2]    r7
  95.     jmpbuf[3]    r8
  96.     jmpbuf[4]    r9
  97.     jmpbuf[5]    r10
  98.     jmpbuf[6]    r11
  99.     jmpbuf[7]    r12
  100.     jmpbuf[8]    r13
  101.     jmpbuf[9]    r14
  102.     jmpbuf[10]    r15    return address
  103.     jmpbuf[11]        saved sigmask
  104.     jmpbuf[12] - jmpbuf[15] unused
  105. */
  106.  
  107. struct TT_D_COM    {        // structure of AOS 4.3 trace table
  108.     unsigned magic1:8,    // = 0xDF
  109.         code:8,        // = 7
  110.         magic2:8,    // = 0xDF
  111.         first_gpr:4,    // first general register saved
  112.         optw:1,    optx:1,    opty:1, :1;   // option flags
  113.     char    npars:4,    // number of parameters
  114.         frame_reg:4;    // frame pointer register number
  115.     char    fpr_save:8;    // saved fp register mask (if opty==1)
  116.     char    lcl_off_size:2,    // size of lcl_offset
  117.         lcl_offset1:6,    // # words to top of stack frame from frame_reg
  118.         lcl_offsetn[3];    // 6, 14, 22, or 30 bits
  119. };
  120.  
  121. // Find trace table
  122.     register short* p = (short*)PC();
  123.     while (((*p++ & 0xff00) != 0xdf00) || ((*p & 0xff00) != 0xdf00));
  124.     TT_D_COM* ttp = (TT_D_COM*)--p;
  125.     if (ttp->code != 7) {
  126.         setNIHCLerror(NIHCL_BADTRACETBL,FATAL,ttp,className(),this);
  127.         return;
  128.     }
  129. // Adjust frame pointer register
  130.     if (ttp->frame_reg >= 6) env[ttp->frame_reg-6+1] += delta;
  131. // Adjust stack pointer register
  132.     SP() += delta;
  133. #endif
  134.  
  135. #ifdef SYSVR2
  136.     SP() += delta;
  137. #endif
  138.  
  139. #endif
  140.  
  141. }
  142.  
  143. unsigned HeapProc::size() const   { return stack_size; }
  144.  
  145. void HeapProc::switchContext(Process* new_process)
  146. {
  147. // check for stack overflow
  148. #if STACK_GROWS_DOWN
  149.     if (topOfStack() < stack_bottom || stack_bottom[0] != (stackTy)UNINITIALIZED)
  150.         setError(NIHCL_STACKOV,DEFAULT,name());
  151. #else
  152.     if (topOfStack() >= stack_bottom+stack_size || stack_bottom[stack_size-1] != (stackTy)UNINITIALIZED)
  153.         setError(NIHCL_STACKOV,DEFAULT,name());
  154. #endif
  155.     save();
  156.     new_process->switchFrom(this);
  157. }
  158.  
  159. void HeapProc::switchFrom(HeapProc*)
  160. // switch from active HeapProc to next HeapProc
  161. {
  162.     LONGJMP(env, Scheduler::resume_new_process);
  163. }
  164.  
  165. void HeapProc::switchFrom(StackProc*)
  166. // switch from active StackProc to next HeapProc
  167. {
  168.     LONGJMP(env, Scheduler::resume_new_process);
  169. }
  170.